package com.developlibrary.socket;

import java.net.*;
import java.io.*;

public class Client{
	
	public void send(){
	
		String host = "60.247.106.213";
		int port = 8888;
		char[] bodyBuf = new char[16];
		Socket socket = null;
		int recvBytes=0;
		String str = "0000000000000004guid";
		String msg="";
		BufferedReader socketInputReader;
		BufferedWriter socketOutputWriter;

		try{

			socket = new Socket(host,port);
			socketInputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			socketOutputWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			socketOutputWriter.write(str);
			socketOutputWriter.flush();
			while(recvBytes!=-1){
				recvBytes = socketInputReader.read(bodyBuf,0,16);
				msg+=new String(bodyBuf,0,recvBytes);
				//System.out.println(new String(bodyBuf,0,recvBytes));
				if(recvBytes<16){
					break;
				}
			}
			socketOutputWriter.close();
			socketInputReader.close();
			System.out.println(msg);
			System.out.println(msg.length());




			/*
			PrintWriter pw = new PrintWriter(socket.getOutputStream());
			//BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			pw.println("0000000000000004guid");
			pw.write("0000000000000004guid");
			System.out.println("I:"+br);
			pw.flush();
			String r = br.readLine();
			pw.close();
			br.close();
			*/
			/*
			OutputStream out = new OutputStream();
			out = socket.getOutputStream();
			InputStream in = socket.getInputStream();
			out.write(s);
			String  r = in.read();
			System.out.println(r);
			

			out.close();
			in.close();
			*/

			/*
			   byte buff[] = new byte[1024];
		   	   Socket s = new Socket(host,port);
    			   OutputStream output = s.getOutputStream();
			   InputStream input = s.getInputStream();
			   String GetCmd = "GET /index.html HTTP/1.0\r\n\r\n";
			   GetCmd.getBytes(0,GetCmd.length(),buff,0);
			   output.write(buff);
			   input.read(buff,0,buff.length);
			   System.out.println(new String(buff,0));

			   */

			                                                                                                                                                                                   
		}catch(Exception e){
			e.printStackTrace();
		}

	
		finally{
			try{
				socket.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}

	public static void main(String argv[]){
		System.out.println("run...");
		Client cl = new Client();
		cl.send();
		System.out.println("end");
	}
}
              
